package main.locations.balaganchik

import main.items.TransferItemAction
import main.items.wallet.TransferMoneyAction
import main.models.{AbstractItem, StateAction}
import main.person.Person
import main.state.{State, StateLens}

case class BuyAction(balaganchik: StateLens[Balaganchik], buyer: StateLens[Person], item: Class[? <: AbstractItem]) extends StateAction {
  override def apply(implicit state: State): State = {

    require(balaganchik.get.owner != null, "Balaganchik must have owner")
    val itemLens = balaganchik.get.items.filter(a => a.get.getClass == item).head
    val state1 = state(TransferItemAction(balaganchik.get(state).owner, buyer, itemLens))
    state1(TransferMoneyAction(buyer, balaganchik.get(state1).owner, balaganchik.get(state1).price))
  }
}
